<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI Web Develop
 *
 * @package     HITI Web Dev  
 * @category    Models
 * @see 		Hiti Ajax
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Hiti_Ajax_model extends CI_Model {
	public function __contruct()
	{
		parent::__contruct();
	}

	public function add_comment($data)
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->insert('comments', $data);
		return true;
	}

	public function add_contact($data)
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->insert('contacts', $data);
		return true;
	}

	public function add_subscriber($data)
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->insert('subscribers', $data);
		return true;
	}

	public function add_member($data)
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->insert('members', $data);
		return true;
	}

	public function save_profile($data, $email = '')
	{
		if (!is_array($data))
		{
			return false;
		}
		if (!$email)
		{
			$email = $this->input->cookie('mem_email', true);
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('email', $email);
		$this->db->update('members', $data);
		return true;
	}

	public function login($email, $password)
	{
		$this->db->select('id, name, avartar');
		$this->db->where('email', $email);
		$this->db->where('password', $password);
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$query = $this->db->get('members');
		if ($query->num_rows())
		{
			return $query->row_array();
		}
		else
		{
			return 0;
		}
	}

	public function check_email_exists($email, $table = 'members')
	{
		$this->db->select('id');
		$this->db->where('email', $email);
		$this->db->where('deleted', 0);
		return $this->db->get($table)->num_rows();
	}

	public function add_cart($id, $sl, $color, $kthuoc)
	{
		if ($sl == '' || $sl == 0)
		{
			$sl = 1;
		}
		$this->db->select('id, soluong, pricelist');
		$this->db->where('product', $id);
		$this->db->where('ip', $this->input->ip_address());
		$this->db->where('color', $color);
		$this->db->where('kthuoc', $kthuoc);
		$query = $this->db->get('shopping_carts');
		if ($query->num_rows())
		{
			$cart = $query->row();
			$sl = $cart->soluong + $sl;
			$gia = get_price($id, $sl, $cart->pricelist);
			$cartid = $cart->id;
			$this->db->where('id', $cartid);
			$this->db->update('shopping_carts', array('soluong' => $sl, 'gia' => $gia));
		}
		else
		{
			$this->db->select('id, pricelist');
			$this->db->where('id', $id);
			$query = $this->db->get('products');
			if ($query->num_rows())
			{
				$product = $query->row();
				$pricelist = $product->pricelist;
				$gia = get_price($id, $sl, $pricelist);
				$this->db->insert('shopping_carts', array('product' => $id, 'ip' => $this->input->ip_address(), 'soluong' => $sl, 'gia' => $gia, 'color' => $color, 'kthuoc' => $kthuoc, 'pricelist' => $pricelist));
			}
		}
		$this->db->select('id')->where('ip', $this->input->ip_address());
		return $this->db->get('shopping_carts')->num_rows();
	}

	public function num_cart()
	{
		$this->db->select('id')->where('ip', $this->input->ip_address());
		return $this->db->get('shopping_carts')->num_rows();
	}

	public function remove_cart($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('shopping_carts');
		$this->db->where('ip', $this->input->ip_address());
		$query = $this->db->get('shopping_carts');
		$total = 0;
		$numCart = $query->num_rows();
		if ($numCart)
		{
			foreach ($query->result_array() as $data)
			{
                $total += $data['gia'] * $data['soluong'];
			}
		}
		$jsonArr = array(
            'numCart' => $numCart,
            'total' => number_format($total)
        );
		return json_encode($jsonArr);
	}

	public function update_cart($id, $qty)
	{
		$this->db->select('product, pricelist, gia');
		$this->db->where('id', $id);
		$this->db->where('ip', $this->input->ip_address());
		$query = $this->db->get('shopping_carts');
		$cart = $query->row();
		$gia = $cart->gia;
		$this->db->where('id', $id);
		$this->db->update('shopping_carts', array('soluong' => $qty, 'gia' => $gia));
		$this->db->where('ip', $this->input->ip_address());
		$query = $this->db->get('shopping_carts');
		$total = 0;
		$thanhtien = 0;
		$numCart = $query->num_rows();
		if ($numCart)
		{
			foreach ($query->result_array() as $data)
			{
				if ($data['id'] == $id) $thanhtien = $data['gia'] * $data['soluong'];
                $total += $data['gia'] * $data['soluong'];
			}
		}
		$jsonArr = array(
            'thanhtien' => number_format($thanhtien),
            'total' => number_format($total)
        );
		return json_encode($jsonArr);
	}

	public function empty_cart()
	{
		$this->db->where('ip', $this->input->ip_address());
		$this->db->delete('shopping_carts');
		return true;
	}

	public function load_city($parent)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, name_vn, price');
		$this->db->where('parent', $parent);
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$query = $this->db->get('regions');
		if ($query->num_rows())
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function hoantat_hoadon($data)
	{
		if (!is_array($data))
		{
			return false;
		}
		$total = 0;
		$this->db->where('ip', $this->input->ip_address());
		$query = $this->db->get('shopping_carts');
		if ($query->num_rows())
		{
            foreach ($query->result_array() as $cart)
            {
                $tongcong = $cart['gia'] * $cart['soluong'];
                $order = array(
                	'tongcong' => $tongcong,
                	'proid' => $cart['product'],
                	'gia' => $cart['gia'],
                	'soluong' => $cart['soluong'],
                	'giam_khohang' => '',
                	'pricelist' => $cart['pricelist'],
                	'ma' => $data['ma']
                );
                $this->db->insert('orders_details', $order);
            }
        }
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->insert('orders', $data);
		$this->db->where('ip', $this->input->ip_address());
		$this->db->delete('shopping_carts');
	}

	public function order_confirm($ma, $maxacthuc)
	{
		$this->db->select('id');
		$this->db->where('ma', $ma);
		$this->db->where('maxacthuc', $maxacthuc);
		$query = $this->db->get('orders');
		if ($query->num_rows())
		{
			$this->db->where('ma', $ma);
			$this->db->where('maxacthuc', $maxacthuc);
			$this->db->update('orders', array('status' => 1));
			return 1;
		}
		else
		{
			return 0;
		}
	}

}

/* End of file hiti_ajax_model.php */
/* Location: ./application/models/hiti_ajax_model.php */